package com.kamistoat.meimeichat.meimeichatserverapplication;

import com.kamistoat.meimeichat.meimeichatserverapplication.kcp.KcpNettyServer;
import com.kamistoat.meimeichat.meimeichatserverapplication.netty.NettyServer;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;

@SpringBootApplication
public class MeiMeiChatServerDddApplication implements CommandLineRunner {

    private static final Logger LOGGER = LoggerFactory.getLogger(MeiMeiChatServerDddApplication.class);

    @Autowired
    NettyServer nettyServer;
    @Autowired
    KcpNettyServer kcpNettyServer;

    @Qualifier(value = "nettyThreadPoolExecutor")
    @Autowired
    ThreadPoolExecutor nettyThreadPoolExecutor;

    @Value("${server.port}")
    Integer serverPort;
    @Value("${netty.port}")
    Integer TCPServerPort;
    @Value("${netty.UDP_port}")
    Integer UDPServerPort;


    public static void main(String[] args) {
        SpringApplication.run(MeiMeiChatServerDddApplication.class, args);
        LOGGER.info("(♥◠‿◠)ﾉﾞ  MeiMeiChat服务端启动成功   ლ(´ڡ`ლ)ﾞ  \n");
        System.out.println(
                "/$$      /$$             /$$     /$$      /$$             /$$   \n" +
                        "| $$$    /$$$            |__/    | $$$    /$$$            |__/  \n" +
                        "| $$$$  /$$$$   /$$$$$$   /$$    | $$$$  /$$$$   /$$$$$$   /$$  \n" +
                        "| $$ $$/$$ $$  /$$__  $$ | $$    | $$ $$/$$ $$  /$$__  $$ | $$  \n" +
                        "| $$  $$$| $$ | $$$$$$$$ | $$    | $$  $$$| $$ | $$$$$$$$ | $$  \n" +
                        "| $$\\  $ | $$ | $$_____/ | $$    | $$\\  $ | $$ | $$_____/ | $$  \n" +
                        "| $$ \\/  | $$ |  $$$$$$$ | $$    | $$ \\/  | $$ |  $$$$$$$ | $$  \n" +
                        "|__/     |__/  \\_______/ |__/    |__/     |__/  \\_______/ |__/ ");
    }

    /**
     * Callback used to run the bean.
     *
     * @param args incoming main method arguments
     * @throws Exception on error
     */
    @Override
    public void run(String... args) throws Exception {
        LOGGER.info("Netty Server TCP启动服务开始 port：{}", TCPServerPort);
        LOGGER.info("Netty Server UDP Listener服务开始 port：{}", UDPServerPort);

        // 1. 启动TCP链接服务
        Future<ChannelFuture> nettyChannelFuture = nettyThreadPoolExecutor.submit(nettyServer);
        Channel channel = nettyChannelFuture.get().channel();
        if (null == channel) {
            throw new RuntimeException("netty server start error: channel is null");
        }

        // 2. 启动UDP监听服务
        Future<ChannelFuture> kcp_nettyChannelFuture = nettyThreadPoolExecutor.submit(kcpNettyServer);
        Channel kcp_channel = kcp_nettyChannelFuture.get().channel();
        if (null == kcp_channel) {
            throw new RuntimeException("netty UDP listener start error: channel is null");
        }

        while (!channel.isActive()) {
            LOGGER.info("Netty Server启动服务ing ...");
            Thread.sleep(3000);
        }

        while (!kcp_channel.isActive()) {
            LOGGER.info("Netty UDP Listener 启动服务ing ...");
            Thread.sleep(3000);
        }

        LOGGER.info("NettyServer启动服务完成, Netty TCP 链接地址: {}", channel.localAddress());
        LOGGER.info("Netty UDP Listener 监听服务启动完成，Netty UDP Listener 链接地址: {}", kcp_channel.localAddress());
        LOGGER.info("NettyServer启动服务完成, Web端口: {}", serverPort);
    }
}
